#!/bin/sh

# 仅支持mp4文件 h264 硬编码 的推流版本.
# 支持裸视频音频帧的输入输出.
#

# +++ 变量声明 Start+++
export g_libId="ffmpeg-6.0"
# 源码-库目录
export g_sourceLibDir=${g_inputRootDir}/${g_libId}
# 脚本-库目录
g_scriptLibDir=${g_scriptArchDir}/${g_libId}
# Build-库目录
export g_buildLibDir=${g_buildArchDir}/${g_libId}
if [[ ! -e ${g_buildLibDir} ]]; then
    mkdir -p ${g_buildLibDir}
fi

# Output-库目录
export g_outputLibDir=${g_outputArchDir}/${g_libId}
if [[ ! -e ${g_outputLibDir} ]]; then
    mkdir -p ${g_outputLibDir}
fi
smallSize=$FLAG_YES
export g_flag=""

configCommon() {
    # 输出目录
    g_flag="--prefix=${g_outputLibDir}"
    # 设置pkg,注意--pkg-config的值不要设置错了.这个是默认开启的.
    g_flag="${g_flag} --pkg-config-flags=--static --pkg-config=pkg-config"
    # 各种协议
    g_flag="${g_flag} --enable-gpl --enable-nonfree --enable-version3 --enable-neon"
    g_flag="${g_flag} --enable-pthreads"

    # 库的类型
    g_flag="${g_flag} --disable-shared --enable-static"

    if [[ $smallSize == $FLAG_YES ]]; then
        # 取消所有东西,这个打开,有某些依赖无法满足不能编译出来.
        # g_flag="${g_flag} --disable-everything --disable-autodetect --disable-all"
        # 关闭doc
        g_flag="${g_flag} --disable-doc"
    fi
    # 关闭ffplay,ffprobe,ffmpeg.
    g_flag="${g_flag} --disable-ffplay --disable-sdl2 --disable-ffprobe --disable-ffmpeg"
    # 不适用libx264
    # g_flag="${g_flag} --enable-libx264"
    g_flag="${g_flag} --enable-videotoolbox"

    # 交叉编译
    # g_flag+=" --target-os=darwin"
    # g_flag+=" --enable-cross-compile"
    # g_flag+=" --arch=$g_arch"

    # g_flag+=" --cc=\"xcrun -sdk iphoneos  clang -target arm64-apple-ios10.0\""

    g_flag+=" --disable-x86asm"
    

}
configDemuxer() {
    if [[ $smallSize == $FLAG_YES ]]; then
        g_flag="${g_flag} --disable-demuxers"
        g_flag="${g_flag}  --enable-demuxer=mjpeg"
        g_flag="${g_flag}  --enable-demuxer=aac"
        g_flag="${g_flag}  --enable-demuxer=amr"
        g_flag="${g_flag}  --enable-demuxer=avi"
        g_flag="${g_flag}  --enable-demuxer=mov"
        g_flag="${g_flag}  --enable-demuxer=mpegts"
        g_flag="${g_flag}  --enable-demuxer=mpegvideo"
    fi
}

configParser() {
    if [[ $smallSize == $FLAG_YES ]]; then
        g_flag="${g_flag} --disable-parsers"
        g_flag="${g_flag} --enable-parser=png"
        g_flag="${g_flag} --enable-parser=aac"
        g_flag="${g_flag} --enable-parser=h264"
        g_flag="${g_flag} --enable-parser=mjpeg"
        g_flag="${g_flag} --enable-parser=hevc"
        g_flag="${g_flag} --enable-parser=mpeg4video"
        g_flag="${g_flag} --enable-parser=flac"
        g_flag="${g_flag} --enable-parser=vorbis"
        g_flag="${g_flag} --enable-parser=mpegaudio"
        g_flag="${g_flag} --enable-parser=mpegvideo"
    fi
}

configEncoder() {
    if [[ $smallSize == $FLAG_YES ]]; then
        g_flag="${g_flag} --disable-encoders"
        g_flag="${g_flag} --enable-encoder=rawvideo"
        g_flag="${g_flag} --enable-encoder=pcm_s16le"
        # g_flag="${g_flag} --enable-encoder=png"
        # g_flag="${g_flag} --enable-encoder=gif"
        # g_flag="${g_flag} --enable-encoder=libx264"
        g_flag="${g_flag} --enable-encoder=mpeg4"
        # g_flag="${g_flag} --enable-encoder=libhw_avc"
        g_flag="${g_flag} --enable-encoder=h264_videotoolbox"
        # g_flag="${g_flag} --enable-encoder=hevc_videotoolbox"
        # g_flag="${g_flag} --enable-encoder=mjpeg"
        g_flag="${g_flag} --enable-encoder=aac"
        # g_flag="${g_flag} --enable-encoder=mp3"
        g_flag="${g_flag} --enable-encoder=wrapped_avframe"
        g_flag="${g_flag} --enable-encoder=flv"
    fi
}

configMuxer() {
    if [[ $smallSize == $FLAG_YES ]]; then 
    g_flag="${g_flag} --disable-muxers"
    g_flag="${g_flag} --enable-muxer=nut"
    g_flag="${g_flag} --enable-muxer=null"
    g_flag="${g_flag} --enable-muxer=mp4"
    g_flag="${g_flag} --enable-muxer=flv"
    g_flag="${g_flag} --enable-muxer=mjpeg"
    g_flag="${g_flag} --enable-muxer=mpegts"
    g_flag="${g_flag} --enable-muxer=rtsp"
    g_flag="${g_flag} --enable-muxer=hls"
    # 裸视频输出
    g_flag="${g_flag} --enable-muxer=rawvideo"
    # 裸音频输出
    g_flag="${g_flag} --enable-muxer=pcm_s16le"
    fi
}

configProtocol() {
    if [[ $smallSize == $FLAG_YES ]]; then 
    g_flag="${g_flag} --disable-protocols"
    g_flag="${g_flag} --enable-protocol=file"
    fi
}

configDecoder() {
    if [[ $smallSize == $FLAG_YES ]]; then 
    g_flag="${g_flag}  --disable-decoders"
    g_flag="${g_flag} --enable-decoder=mp3"
    g_flag="${g_flag} --enable-decoder=aac"
    g_flag="${g_flag} --enable-decoder=mjpeg"
    g_flag="${g_flag} --enable-decoder=h264"
    g_flag="${g_flag} --enable-decoder=mpeg4"
    g_flag="${g_flag} --enable-decoder=hevc"
    g_flag="${g_flag} --enable-decoder=ac3"
    g_flag="${g_flag} --enable-decoder=rawvideo"
    g_flag="${g_flag} --enable-decoder=pcm_alaw"
    g_flag="${g_flag} --enable-decoder=pcm_bluray"
    g_flag="${g_flag} --enable-decoder=pcm_dvd"
    g_flag="${g_flag} --enable-decoder=pcm_f16le"
    g_flag="${g_flag} --enable-decoder=pcm_f24le"
    g_flag="${g_flag} --enable-decoder=pcm_f32le"
    g_flag="${g_flag} --enable-decoder=pcm_f32be"
    g_flag="${g_flag} --enable-decoder=pcm_f64be"
    g_flag="${g_flag} --enable-decoder=pcm_f64le"
    g_flag="${g_flag} --enable-decoder=pcm_lxf"
    g_flag="${g_flag} --enable-decoder=pcm_mulaw"
    g_flag="${g_flag} --enable-decoder=pcm_s16be"
    g_flag="${g_flag} --enable-decoder=pcm_s16be_planar"
    g_flag="${g_flag} --enable-decoder=pcm_s16le"
    g_flag="${g_flag} --enable-decoder=pcm_s16le_planar"
    g_flag="${g_flag} --enable-decoder=pcm_s24be"
    g_flag="${g_flag} --enable-decoder=pcm_s24daud"
    g_flag="${g_flag} --enable-decoder=pcm_s24le"
    g_flag="${g_flag} --enable-decoder=pcm_s24le_planar"
    g_flag="${g_flag} --enable-decoder=pcm_s32be"
    g_flag="${g_flag} --enable-decoder=pcm_s32le"
    g_flag="${g_flag} --enable-decoder=pcm_s32le_planar"
    g_flag="${g_flag} --enable-decoder=pcm_s64be"
    g_flag="${g_flag} --enable-decoder=pcm_s64le"
    g_flag="${g_flag} --enable-decoder=pcm_s8"
    g_flag="${g_flag} --enable-decoder=pcm_s8_planar"
    g_flag="${g_flag} --enable-decoder=pcm_u16be"
    g_flag="${g_flag} --enable-decoder=pcm_u16le"
    g_flag="${g_flag} --enable-decoder=pcm_u24be"
    g_flag="${g_flag} --enable-decoder=pcm_u24le"
    g_flag="${g_flag} --enable-decoder=pcm_u32be"
    g_flag="${g_flag} --enable-decoder=pcm_u32le"
    g_flag="${g_flag} --enable-decoder=pcm_u8"
    g_flag="${g_flag} --enable-decoder=pcm_vidc"
    g_flag="${g_flag} --enable-decoder=wrapped_avframe"
    fi
}
configIndev() {
    if [[ $smallSize == $FLAG_YES ]]; then 
        g_flag="${g_flag} --disable-indevs"
    fi
}
configFilters() {
    if [[ $smallSize == $FLAG_YES ]]; then 
        g_flag+=" --disable-filters"
    fi
    # g_flag="${g_flag} --disable-filters --enable-filter=null"
}
configOutdev() {
    echo ""
    # g_flag="${g_flag} --enable-outdev=sdl2"
}
configBsf() {
    if [[ $smallSize == $FLAG_YES ]]; then 
        g_flag+=" --disable-bsfs"
    fi
}
configHwaccel() {
    echo ""
    # 加速
    # g_flag="${g_flag} --disable-hwaccels"
}
configDebug() {
    if [[ $g_compileMode == $COMPILE_MODE_DEBUG ]]; then
        g_flag="${g_flag} --disable-stripping --disable-optimizations"
    else
        g_flag="${g_flag} --enable-optimizations --disable-debug"
    fi
}

configCommon
configProtocol
configParser
configDecoder
configDemuxer
configIndev
configOutdev
configFilters
configBsf
configHwaccel
configMuxer
configEncoder
configDebug

function customConfigure {
    ${g_sourceLibDir}/configure ${g_flag} \
    --cc="$g_cc"
}

#自定义make,将所有文件打包为libffmpeg.a
function customMake {
    make -j4
    installHeader
    installToFfmpegFramework
}

function installHeader {
    # 导出默认头文件
    make install-headers 
    # 导出额外需要的头文件.
    # input/ffmpeg/
    src=$g_sourceLibDir
    # output/ffmpeg/include
    dst=$g_outputLibDir/include
    echo "copying extra header file:\n$src\n==>\n$dst"
    headers=(
        "libavutil/thread.h" 
         "libavutil/libm.h"
        "libavutil/getenv_utf8.h"
        "libavutil/internal.h"
        "libavutil/timer.h"
        "libavutil/attributes_internal.h"
        "libavutil/x86/timer.h"
        "libavutil/x86/emms.h"
        "libavutil/x86/asm.h"
        "compat/va_copy.h"
        "libavcodec/mathops.h"
        "libavcodec/x86/mathops.h"
        "libavformat/os_support.h"
     )
    for file in ${headers[@]} 
    do
        echo $file
        # 单纯cp目标文件夹不存在会导致失败,首先创建文件夹.
        dstDir=`dirname $dst/$file`
        mkdir -p $dstDir
        cp $src/$file $dst/$file
    done
}

#默认的安装方式,每个模块生成对应的库
function installDefault {
    make install
}

#将所有.o文件包含到1个.a库中.
function installToFfmpegA {
     _AR="xcrun -sdk macosx ar"
    find ./ -name "*.o"|xargs $_AR rcs libffmpeg.a
    [[ ! -e $g_outputLibDir/lib ]] && mkdir -p $g_outputLibDir/lib
	cp -f libffmpeg.a ${g_outputLibDir}/lib/
}

# 生成静态framework.
function installToFfmpegFramework {
    installToFfmpegA
    mkdir -p  $g_outputLibDir/ffmpeg.framework
    if [[ ! -e $g_outputLibDir/ffmpeg.framework/headers ]]; then
        mv $g_outputLibDir/include $g_outputLibDir/ffmpeg.framework/headers
    fi
    cp $g_outputLibDir/lib/libffmpeg.a ${g_outputLibDir}/ffmpeg.framework/ffmpeg
    # 拷贝ffmpeg.framework到额外需要使用的地方.
    libName=${g_libId%%-*}
    echo $libName
    extraOutputDirArray=$(yq '.libArray.ffmpeg.extraOutputDir[]' $g_projectDir/config.yml)
    for dir in ${extraOutputDirArray[@]} 
    do
        echo "extra copy dir is: $dir"
        cp -rf ${g_outputLibDir}/ffmpeg.framework $dir
    done
}